module;

#include <boost/asio/ip/tcp.hpp>

export module Modbus;
import std;
import Config;
import Bytes;
import <boost/crc.hpp>;

using namespace boost;
using tcp = asio::ip::tcp;

export namespace Modbus {
	void test_main() {
		auto msg = 0x0101000100032DCB;
		Bytes::Bytes<8> arr{ msg };
		std::cout << arr.to_string_hex() << std::endl;
		Bytes::Bytes<8> arr2(0x01, 0x01, 0x0001, 0x0003);
		std::cout << arr2.to_string_hex() << std::endl;
	}
	class Client {
	private:
		std::ostream& log = std::clog;
		std::ostream& err = std::cerr;
		
		std::string modbus_server_ip = "192.168.0.7";
		std::string modbus_server_port = "8080";

		asio::io_context ioc;
		tcp::iostream stream_modbus;
	public:
		bool tryConnect() {
			log << Config::try_connect << std::endl;
			stream_modbus = tcp::iostream(modbus_server_ip, modbus_server_port);
			if (!stream_modbus) {
				err << Config::bad_connect << std::endl;
				return false;
			}
			log << Config::yes_connect << std::endl;
			return true;
		}
		void sendOne(std::int64_t a64) {
			Bytes::Bytes<8> arr{ a64 };
			stream_modbus << arr;
			std::cout << stream_modbus.rdbuf();
		}
		void start() {
			if (tryConnect()) {
				return;
			}
			auto msg = 0x0101000100032DCB;

			sendOne(msg);
		}
	};
}